package org.example.service.impl;

import jakarta.annotation.Resource;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.example.dao.BookDao;
import org.example.pojo.Book;
import org.example.service.IBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author TonySong
 * @date 2024/5/14 0014
 * @time 16:10
 */
@Service
public class BookService implements IBookService {
    @Resource
    private BookDao bookDao;


    @Trace
    @Tag(key = "getBooks", value = "returnedObj")
    @Override
    public List<Book> getBooks() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return this.bookDao.findAll();
    }

    @Trace
    @Tags({@Tag(key = "getBookById", value = "returnedObj"),
            @Tag(key = "getBookById", value = "args[0]")})
    @Override
    public Book getBookById(Long id) {
        Optional<Book> book = this.bookDao.findById(id);
         if(book.isPresent()) {
             return book.get();
         }
         return null;

    }

    @Override
    public void updateAmount(Long bid) {
        this.bookDao.updatpAmount(bid);
    }

    @Trace
    @Tag(key = "show", value = "returnedObj")
    @Override
    public String show() {
        return "测试自定义链路追踪！";
    }
}
